/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kungkhies.domain.kungkher;

import com.kungkhies.persistence.kungkher.KungkherDao;
import com.kungkhies.services.kungkher.KungkherService;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 *
 * @author clayton
 */
@Component
//@Service("kungkherService")
public class KungkherRegoFormObjectValidator implements Validator
{
    public static final Pattern ALPHANUMERIC = Pattern.compile("[A-Za-z0-9]+");
    
    private KungkherService kungkherService;

    @Override
    public boolean supports(Class<?> type)
    {
        return KungkherRegoFormObject.class.equals(type);
    }

    @Override
    public void validate(Object o, Errors errors)
    {

        ValidationUtils.rejectIfEmpty(errors, "fullName", "fullName.empty");
        ValidationUtils.rejectIfEmpty(errors, "kungkherName", "kungkherName.empty");
        ValidationUtils.rejectIfEmpty(errors, "password", "password.empty");
        ValidationUtils.rejectIfEmpty(errors, "confirmPassword", "confirmPassword.empty");
        ValidationUtils.rejectIfEmpty(errors, "email", "email.empty");
        ValidationUtils.rejectIfEmpty(errors, "confirmEmail", "confirmEmail.empty");

        KungkherRegoFormObject k = (KungkherRegoFormObject) o;
        
        if (kungkherService == null )
        {
            System.out.println("kungkherService is NULL in validator.");
        }
        else
        {
            System.out.println("kungkherService is NOT NULL in validator.");
        }

        if (errors != null && !errors.hasFieldErrors())
        {
            if (k.getFullName().length() > 15)
            {
                errors.rejectValue("fullName", "fullName.outofrange");
            } 
            else if (k.getPassword().length() > 15)
            {
                errors.rejectValue("password", "password.outofrange");
            }
            else if (k.getKungkherName().length() > 15)
            {
                errors.rejectValue("kungkherName", "kungkherName.outofrange");
            }
            else if (!hasValidCharacters(k.getKungkherName()))
            {
                errors.rejectValue("kungkherName", "kungkherName.illegal.characters");
            }
            else if (!hasValidCharacters(k.getFullName()))
            {
                errors.rejectValue("fullName", "fullName.illegal.characters");
            }
            else if (!k.getPassword().equals(k.getConfirmPassword()))
            {
                //<editor-fold defaultstate="collapsed" desc="comment">
                errors.rejectValue("password", "password.mismatch");
            }
            else if (kungkherService.kungkherNameExists(k.getKungkherName()))
            {
                errors.rejectValue("kungkherName", "kungkherName.exists");
            }
            else if (!hasValidEmail(k.getEmail()))
            {
                errors.rejectValue("email", "email.invalid");
            }
            else if (!k.getEmail().equals(k.getConfirmEmail()))
            {
                errors.rejectValue("email", "email.mismatch");
            }
            //</editor-fold>
            else if (kungkherService.emailAddressExists(k.getEmail()))
            {
                errors.rejectValue("email", "email.exists");
            }

        }

    }

    public boolean hasValidCharacters(String formData)
    {
        Matcher m = ALPHANUMERIC.matcher(formData);
        return m.matches();
    }

    public boolean hasValidEmail(String email)
    {
        if (email != null)
        {
            if (email.contains("@"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    @Autowired
    public void setKungkherService(KungkherService kungkherService)
    {
        this.kungkherService = kungkherService;
    }
}
